package com.ponhu.ea2.goodsCenter.provider.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.dto.PhInventoryForGoodsCenterDto;
import com.ponhu.ea2.entity.Inventory;
import com.ponhu.ea2.enums.InventoryChannelEnum;
import com.ponhu.ea2.enums.SyncAppletTypeEnum;
import com.ponhu.ea2.goodsCenter.provider.factory.GoodsFactory;
import com.ponhu.ea2.goodsCenter.provider.factory.GoodsFactoryService;
import com.ponhu.ea2.goodsCenter.provider.mapper.InventoryMapper;
import com.ponhu.ea2.properties.GoodsProperties;
import com.ponhu.ea2.service.facade.GoodsCenterForChuangjiService;
import com.ponhu.ea2.service.facade.GoodsCenterForPonHuService;
import com.ponhu.ea2.service.facade.GoodsCenterForPurchaseService;
import com.ponhu.ea2.service.facade.GoodsCenterForWMSService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/**
 * @author luyu
 * @create 2021-11-04 11:21
 */
@Slf4j
@Service
public class PhInventoryForUpdateByGoodsCenterService implements GoodsFactoryService<PhInventoryForGoodsCenterDto> {

    @Resource
    private GoodsCenterForPonHuService goodsCenterForPonHuService;

    @Resource
    private GoodsCenterForWMSService goodsCenterForWMSService;

    @Resource
    private GoodsCenterForPurchaseService goodsCenterForPurchaseService;

    @Resource
    private GoodsCenterForChuangjiService goodsCenterForChuangjiService;

    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    GoodsProperties goodsProperties;

    @Resource
    PhinventoryToAmqpService phinventoryToAmqpService;

    @Override
    public void afterPropertiesSet() throws Exception {
        GoodsFactory.setMap(this);
    }

    @Override
    public ResponseBean createOrUpdateGoods(PhInventoryForGoodsCenterDto phInventoryByGoodsForPonhuDto) {
        Boolean bool = true;
        // 调用胖总管修改接口
        if (BooleanUtils.isNotTrue(goodsCenterForPonHuService
                .updateGoodsInfoForPonhu(phInventoryByGoodsForPonhuDto))) {
            bool=false;
        }
        // 根据货品编码查询货品
        Inventory inventory = inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getInventoryCode, phInventoryByGoodsForPonhuDto.getInventoryCode()));
        if (BooleanUtils.isNotTrue(goodsCenterForWMSService.createOrUpdateGoodsForWms(inventory))) {
            bool=false;
        };
        if(!InventoryChannelEnum.CLOUD_TRADE_JOINT_CONSIGNMENT.getCode()
                .equals(inventory.getInventoryChannel())){
            // 同步采购
            log.info("推送采购的开关为:{}",goodsProperties.getEnablePushPurchase());
            if(BooleanUtils.isTrue(goodsProperties.getEnablePushPurchase())){
                log.info("推送采购中心开始");
                goodsCenterForPurchaseService.phInventoryUpdateToPurchase(inventory.getId(),
                        phInventoryByGoodsForPonhuDto.getOperatorName());
                log.info("推送采购结束");
            }
            // 同步创纪云
            log.info("推送创纪云的开关为:{}",goodsProperties.getEnablePushChuangji());
            if(BooleanUtils.isTrue(goodsProperties.getEnablePushChuangji())){
                log.info("推送创纪云开始");
                goodsCenterForChuangjiService.phInventoryUpdateToChuangji(inventory.getId());
                log.info("推送创纪云结束");
            }
        }
        // 同步会员小程序
        CompletableFuture.runAsync(() ->{
            phinventoryToAmqpService.pushDataToAmqp(
                    Arrays.asList(phInventoryByGoodsForPonhuDto.getInventoryCode()),
                    SyncAppletTypeEnum.PH_INVENTORY_DATA.getCode());
        });

        return bool?ResponseBean.ok():ResponseBean.error();
    }
}
